package cn.itcast.controller;

import cn.itcast.domain.Msg;
import cn.itcast.domain.Student;
import cn.itcast.domain.Teacher;
import cn.itcast.service.TeacherService;
import cn.itcast.service.impl.TeacherServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class TeacherController {

    @Autowired
    TeacherServiceImpl teacherService;

    @Autowired
    Teacher teacher;

    // 分页查询
    @RequestMapping(value = "/teaToClassStu")
    @ResponseBody
    public Msg getEmpsWithJson(HttpSession session,
            @RequestParam(value = "pn", defaultValue = "1") Integer pn) {

        PageInfo page = null;
        teacher = (Teacher) session.getAttribute("teacher");
        if (teacher != null) {
            // 这不是一个分页查询
            // 引入PageHelper分页插件
            // 在查询之前只需要调用，传入页码，以及每页的大小
            PageHelper.startPage(pn, 5);
            // startPage后面紧跟的这个查询就是一个分页查询
            teacher = (Teacher) session.getAttribute("teacher");
            List<Student> emps = teacherService.findByAll(teacher.getTno());
            // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
            // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
            page = new PageInfo(emps, 5);
        }
        return Msg.success().add("pageInfo", page);
    }

}
